package iotwisp.school.user.entity;

import iotwisp.base.orm.StringIdBaseEntity;
import iotwisp.base.tree.TreeDataBaseEntity;
import iotwisp.base.util.UUIDGenerator;
import iotwisp.school.user.command.organization.CreateOrganizationSCmd;
import iotwisp.school.user.command.organization.ModifyOrganizationSCmd;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;

import javax.persistence.*;

@SuppressWarnings("serial")
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicUpdate
@DynamicInsert
@Entity
@Table(name = M.TABLE_PREFIX + "orgaization")
public class Organization extends TreeDataBaseEntity {

    @Column(name = "name", length = 64)
    private String name;

    @Column(name = "type", length = 32)
    private String type;

    @Column(name = "address", length = 64)
    private String address;

    @Column(name = "phone", length = 32)
    private String phone;

    @Type(type = "yes_no")
    @Column(name = "is_remove")
    private Boolean remove;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "default_role_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    private Role defaultRole;

    @Transient
    private Integer manholeCoverCount;

    public void create(CreateOrganizationSCmd cmd, Organization parentOrg, Role defaultRole) {

        setId(UUIDGenerator.getUUID());

        setDefaultRole(defaultRole);

        setName(cmd.getName());
        setType(cmd.getType());
        setAddress(cmd.getAddress());
        setPhone(cmd.getPhone());
        setRemove(false);
    }

    public void modify(ModifyOrganizationSCmd cmd, Organization parentOrg, Role defaultRole) {

        setDefaultRole(defaultRole);

        setName(cmd.getName());
        setType(cmd.getType());
        setAddress(cmd.getAddress());
        setPhone(cmd.getPhone());
    }

    public void remove() {
        setRemove(true);
    }
}
